package com.gugee.ins.common.beanutil;

import com.gugee.ins.common.model.mysql.comment.InsComment;
import com.gugee.ins.common.model.spider.comment.EdgeMediaCommentNode;
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 转换生成 InsComment 类
 * @author Huang xin
 */
public class InsCommentConversion {

    private InsCommentConversion(){

    }

    public static InsComment mapper(EdgeMediaCommentNode dto) {
        if (dto == null) {
            return null;
        } else {
            InsComment insComment = new InsComment();
            insComment.setId(dto.getId());
            insComment.setCUid(getCUid(dto));
            insComment.setText(getText(dto));
            insComment.setCreatedAt(dto.getCreatedAt());
            insComment.setWeekDay(getWeekDay(dto));
            insComment.setDayHours(getDayHour(dto));

            return insComment;
        }
    }

    public static long getCUid(EdgeMediaCommentNode dto) {
        if (dto.getOwner() == null || dto.getOwner().getUid() == null) {
            return 0L;
        }
        return dto.getOwner().getUid();
    }

    public static String getText(EdgeMediaCommentNode dto) {
        if (!StringUtils.isEmpty(dto.getText())) {
            if(dto.getText().length() > 50){
                return dto.getText().substring(0,50);
            }else{
                return dto.getText();
            }
        }
        return null;
    }

    public static int getWeekDay(EdgeMediaCommentNode dto) {
        if (dto.getCreatedAt() > 0) {
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(dto.getCreatedAt()), ZoneId.of("UTC")).getDayOfWeek().getValue() - 1;
        }
        return 0;
    }
    public static int getDayHour(EdgeMediaCommentNode dto) {
        if (dto.getCreatedAt() > 0) {
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(dto.getCreatedAt()), ZoneId.of("UTC")).getHour();
        }
        return 0;
    }
}
